package divestoclimb.checklist.data;

import java.util.Date;

import divestoclimb.checklist.storage.DbAdapter;

public class Checklist extends Template {
	
	protected String mTemplateName;	// The name of the template from which this
									// checklist was created
	protected Date mStartDate;
	protected Date mCompletedDate;
	
	public String getTemplateName() {
		return mTemplateName;
	}
	
	public Date getStartDate() { return mStartDate; }
	public Date getCompletedDate() { return mCompletedDate; }

	public void setStartDate() {
		if(mStartDate == null) {
			mStartDate = new Date();
			mDirty = true;
		}
	}

	public void setCompletedDate() {
		if(mCompletedDate == null) {
			mCompletedDate = new Date();
			mDirty = true;
		}
	}
	
	public void clearCompletedDate() {
		mDirty = (mCompletedDate != null);
		mCompletedDate = null;
	}
	
	public Checklist(Category category, String name) {
		super(name);
		setCategory(category);
	}
	
	/**
	 * This is used to build a checklist that was imported from an external source
	 * @param name
	 * @param template_name
	 * @param template_version
	 * @param start
	 * @param completed
	 */
	public Checklist(String name, String template_name, String template_version, Date start, Date completed) {
		super(name);
		mTemplateName=template_name;
		mVersion=template_version;
		mStartDate=start;
		mCompletedDate=completed;
	}

	public Checklist(long id, Category category, String name, String template_name, String template_version, Date start, Date completed) {
		super(id, category, name, template_version);
		mTemplateName=template_name;
		mStartDate=start;
		mCompletedDate=completed;
	}
	
	@Override
	// Save this back to the database
	public boolean save(DbAdapter d) {
		if(! mDirty) { return false; }
		boolean success;
		if(mID == -1) {
			mID = d.createChecklist(this);
			success = mID != -1;
		} else {
			success = d.updateChecklist(this);
		}
		mDirty = ! success;
		return success;
	}

}
